<!-- post_news.html - 新闻发布表单 -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>发布新闻</title>
    <style>
        .form-container {
            max-width: 600px;
            margin: 20px auto;
            padding: 20px;
            border: 1px solid #ddd;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input[type="text"], textarea {
            width: 100%;
            padding: 8px;
        }
        button {
            padding: 10px 20px;
            background: #007bff;
            color: white;
            border: none;
        }
        .preview-table {
            margin-top: 20px;
            width: 100%;
            border-collapse: collapse;
        }
        .preview-table th, .preview-table td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        .preview-table th {
            background-color: #f2f2f2;
        }
    </style>
    <script src="./jquery/jquery3.1.1.js"></script>
    <script src="./bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="form-container">
    <h2>发布新闻</h2>
    <form>
        <div class="form-group">
            <label>新闻标题：</label>
            <input id="title" type="text">
        </div>

        <div class="form-group">
            <label>作者：</label>
            <input id="author" type="text" name="author" required>
        </div>

        <div class="form-group">
            <label>新闻图片：</label>
            <input id="img" type="file" name="image" accept="image/*">
        </div>

        <div class="form-group">
            <label>新闻内容：</label>
            <textarea id="content" name="content" rows="6" required></textarea>
        </div>

        <button id="submit" onclick="add()">发布新闻</button>
    </form>


</div>

<script>
    function add(){
        event.preventDefault(); // 阻止表单的默认提交行为

        let title = document.getElementById("title").value;
        let author = document.getElementById("author").value;
        let img = document.getElementById("img").value;
        let content = document.getElementById("content").value;


        $.ajax({
            url:"news/addNews",
            type:"post",
            dataType:"json",
            contentType: "application/x-www-form-urlencoded",

            data:{
                "title":title,
                "author":author,
                "img":img,
                "content":content

            },
            success:function(data){
                if(data.code == 200){
                   alert("发布成功！");
                }
            }
        })
    }
</script>
</body>
</html>